library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.1.0     ✓ dplyr   1.0.5
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(gganimate)
library(transformr)
library(magick)
## Linking to ImageMagick 6.9.11.57
## Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fftw, ghostscript, x11
time_series_confirmed <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")) %>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region") 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   .default = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
download.file(url="https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv", 
               destfile = "data/time_series_covid19_confirmed_global.csv")
time_series_confirmed <- read_csv("data/time_series_covid19_confirmed_global.csv")%>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   .default = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
head(time_series_confirmed)
## # A tibble: 6 x 417
##   Province_State Country_Region        Lat   Long `1/22/20` `1/23/20` `1/24/20`
##   <chr>          <chr>               <dbl>  <dbl>     <dbl>     <dbl>     <dbl>
## 1 <NA>           Afghanistan          33.9  67.7          0         0         0
## 2 <NA>           Albania              41.2  20.2          0         0         0
## 3 <NA>           Algeria              28.0   1.66         0         0         0
## 4 <NA>           Andorra              42.5   1.52         0         0         0
## 5 <NA>           Angola              -11.2  17.9          0         0         0
## 6 <NA>           Antigua and Barbuda  17.1 -61.8          0         0         0
## # … with 410 more variables: 1/25/20 <dbl>, 1/26/20 <dbl>, 1/27/20 <dbl>,
## #   1/28/20 <dbl>, 1/29/20 <dbl>, 1/30/20 <dbl>, 1/31/20 <dbl>, 2/1/20 <dbl>,
## #   2/2/20 <dbl>, 2/3/20 <dbl>, 2/4/20 <dbl>, 2/5/20 <dbl>, 2/6/20 <dbl>,
## #   2/7/20 <dbl>, 2/8/20 <dbl>, 2/9/20 <dbl>, 2/10/20 <dbl>, 2/11/20 <dbl>,
## #   2/12/20 <dbl>, 2/13/20 <dbl>, 2/14/20 <dbl>, 2/15/20 <dbl>, 2/16/20 <dbl>,
## #   2/17/20 <dbl>, 2/18/20 <dbl>, 2/19/20 <dbl>, 2/20/20 <dbl>, 2/21/20 <dbl>,
## #   2/22/20 <dbl>, 2/23/20 <dbl>, 2/24/20 <dbl>, 2/25/20 <dbl>, 2/26/20 <dbl>,
## #   2/27/20 <dbl>, 2/28/20 <dbl>, 2/29/20 <dbl>, 3/1/20 <dbl>, 3/2/20 <dbl>,
## #   3/3/20 <dbl>, 3/4/20 <dbl>, 3/5/20 <dbl>, 3/6/20 <dbl>, 3/7/20 <dbl>,
## #   3/8/20 <dbl>, 3/9/20 <dbl>, 3/10/20 <dbl>, 3/11/20 <dbl>, 3/12/20 <dbl>,
## #   3/13/20 <dbl>, 3/14/20 <dbl>, 3/15/20 <dbl>, 3/16/20 <dbl>, 3/17/20 <dbl>,
## #   3/18/20 <dbl>, 3/19/20 <dbl>, 3/20/20 <dbl>, 3/21/20 <dbl>, 3/22/20 <dbl>,
## #   3/23/20 <dbl>, 3/24/20 <dbl>, 3/25/20 <dbl>, 3/26/20 <dbl>, 3/27/20 <dbl>,
## #   3/28/20 <dbl>, 3/29/20 <dbl>, 3/30/20 <dbl>, 3/31/20 <dbl>, 4/1/20 <dbl>,
## #   4/2/20 <dbl>, 4/3/20 <dbl>, 4/4/20 <dbl>, 4/5/20 <dbl>, 4/6/20 <dbl>,
## #   4/7/20 <dbl>, 4/8/20 <dbl>, 4/9/20 <dbl>, 4/10/20 <dbl>, 4/11/20 <dbl>,
## #   4/12/20 <dbl>, 4/13/20 <dbl>, 4/14/20 <dbl>, 4/15/20 <dbl>, 4/16/20 <dbl>,
## #   4/17/20 <dbl>, 4/18/20 <dbl>, 4/19/20 <dbl>, 4/20/20 <dbl>, 4/21/20 <dbl>,
## #   4/22/20 <dbl>, 4/23/20 <dbl>, 4/24/20 <dbl>, 4/25/20 <dbl>, 4/26/20 <dbl>,
## #   4/27/20 <dbl>, 4/28/20 <dbl>, 4/29/20 <dbl>, 4/30/20 <dbl>, 5/1/20 <dbl>,
## #   5/2/20 <dbl>, 5/3/20 <dbl>, …
time_series_confirmed_long <- time_series_confirmed %>% 
               pivot_longer(-c(Province_State, Country_Region, Lat, Long),
                            names_to = "Date", values_to = "Confirmed") 
time_series_confirmed_long$Date <- mdy(time_series_confirmed_long$Date)
head(time_series_confirmed_long)
## # A tibble: 6 x 6
##   Province_State Country_Region   Lat  Long Date       Confirmed
##   <chr>          <chr>          <dbl> <dbl> <date>         <dbl>
## 1 <NA>           Afghanistan     33.9  67.7 2020-01-22         0
## 2 <NA>           Afghanistan     33.9  67.7 2020-01-23         0
## 3 <NA>           Afghanistan     33.9  67.7 2020-01-24         0
## 4 <NA>           Afghanistan     33.9  67.7 2020-01-25         0
## 5 <NA>           Afghanistan     33.9  67.7 2020-01-26         0
## 6 <NA>           Afghanistan     33.9  67.7 2020-01-27         0
time_series_confirmed_long%>% 
  group_by(Country_Region, Date) %>% 
  summarise(Confirmed = sum(Confirmed)) %>% 
  filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Confirmed)) + 
    geom_point() +
    geom_line() +
    ggtitle("US COVID-19 Confirmed Cases")
## `summarise()` has grouped output by 'Country_Region'. You can override using the `.groups` argument.

time_series_confirmed_long %>% 
    group_by(Country_Region, Date) %>% 
    summarise(Confirmed = sum(Confirmed)) %>% 
    filter (Country_Region %in% c("China","France","Italy", 
                                "Korea, South", "US")) %>% 
    ggplot(aes(x = Date,  y = Confirmed, color = Country_Region)) + 
    geom_point() +
    geom_line() +
    ggtitle("COVID-19 Confirmed Cases")
## `summarise()` has grouped output by 'Country_Region'. You can override using the `.groups` argument.

time_series_confirmed_long_daily <-time_series_confirmed_long %>% 
    group_by(Country_Region, Date) %>% 
    summarise(Confirmed = sum(Confirmed)) %>% 
    mutate(Daily = Confirmed - lag(Confirmed, default = first(Confirmed )))
## `summarise()` has grouped output by 'Country_Region'. You can override using the `.groups` argument.
time_series_confirmed_long_daily %>% 
    filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Daily, color = Country_Region)) + 
    geom_point() +
    ggtitle("COVID-19 Confirmed Cases")

time_series_confirmed_long_daily %>% 
    filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Daily, color = Country_Region)) + 
    geom_line() +
    ggtitle("COVID-19 Confirmed Cases")

time_series_confirmed_long_daily %>% 
    filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Daily, color = Country_Region)) + 
    geom_smooth() +
    ggtitle("COVID-19 Confirmed Cases")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

time_series_confirmed_long_daily %>% 
    filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Daily, color = Country_Region)) + 
    geom_smooth(method = "gam", se = FALSE) +
    ggtitle("COVID-19 Confirmed Cases")
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

# set eval false to save time on knits
theme_set(theme_bw())
daily_counts <- time_series_confirmed_long_daily %>% 
    filter (Country_Region == "US")
p <- ggplot(daily_counts, aes(x = Date,  y = Daily, color = Country_Region)) + 
      geom_point() +
      ggtitle("Confirmed COVID-19 Cases") +
      geom_point(aes(group = seq_along(Date))) +
      transition_reveal(Date) 
# Some people using a local installation of RStudio may needed to use this line instead
animate(p, renderer = magick_renderer(), end_pause = 15)
anim_save("daily_counts_US.gif", p)

Exercises

#1

time_series_confirmed_long %>% 
    group_by(Country_Region, Date) %>% 
    summarise(Confirmed = sum(Confirmed)) %>% 
    filter (Country_Region %in% c("Brazil","India","Germany", 
                                "Russia", "US")) %>%
    ggplot(aes(x = Date,  y = Confirmed, color = Country_Region)) + 
    facet_wrap(facets = vars(Country_Region), scales="free_y") +
    geom_point() +
    geom_line() +
    ggtitle("COVID-19 Confirmed Cases")
## `summarise()` has grouped output by 'Country_Region'. You can override using the `.groups` argument.

#2

time_series_confirmed_long %>% 
    group_by(Country_Region, Date) %>% 
    summarise(Confirmed = sum(Confirmed)) %>% 
    filter (Country_Region %in% c("Brazil","India","Germany", 
                                "Russia", "US")) %>%
    ggplot(aes(x = Date,  y = Confirmed, color = Country_Region)) + 
    geom_point() +
    geom_line() +
    ggtitle("COVID-19 Confirmed Cases")
## `summarise()` has grouped output by 'Country_Region'. You can override using the `.groups` argument.

#3

download.file(url="https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv", 
               destfile = "data/time_series_covid19_deaths_global.csv")
time_series_deaths <- read_csv("data/time_series_covid19_deaths_global.csv")%>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   .default = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
time_series_deaths_long <- time_series_deaths %>% 
               pivot_longer(-c(Province_State, Country_Region, Lat, Long),
                            names_to = "Date", values_to = "Deaths") 
time_series_deaths_long$Date <- mdy(time_series_deaths_long$Date)
time_series_deaths_long %>% 
    group_by(Country_Region, Date) %>% 
    summarise(Deaths = sum(Deaths)) %>% 
    filter (Country_Region %in% c("Mexico","Canada", "US")) %>% 
    ggplot(aes(x = Date,  y = Deaths, color = Country_Region)) + 
    geom_line() +
    geom_point() +
    ggtitle("COVID-19 Deaths")
## `summarise()` has grouped output by 'Country_Region'. You can override using the `.groups` argument.

#head(time_series_deaths_long)

#4

time_series_deaths_long_daily <- time_series_deaths_long %>% 
    group_by(Country_Region, Date) %>% 
    summarise(Deaths = sum(Deaths)) %>% 
    mutate(Daily = Deaths - lag(Deaths, default = first(Deaths)))
## `summarise()` has grouped output by 'Country_Region'. You can override using the `.groups` argument.
ex4_graph <- time_series_deaths_long_daily %>% 
    filter (Country_Region %in% c("Mexico","Canada", "US")) %>% 
    ggplot(aes(x = Date,  y = Daily, color = Country_Region)) + 
    ggtitle("COVID-19 Confirmed Cases") +
    geom_smooth(method = "gam", se = FALSE) 

ex4_graph
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

#5

ex5_graph <- time_series_deaths_long_daily %>% 
    filter (Country_Region %in% c("Brazil","India","Germany", 
                                "Russia", "US")) %>%
    ggplot(aes(x = Date,  y = Daily, color = Country_Region)) + 
    geom_smooth(method = "gam", se = FALSE) 
    ggtitle("COVID-19 Confirmed Cases")
## $title
## [1] "COVID-19 Confirmed Cases"
## 
## attr(,"class")
## [1] "labels"
ex5_graph
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

#6

daily_counts <- time_series_deaths_long_daily %>% 
    filter (Country_Region %in% c("Brazil","India","Germany", 
                                "Russia", "US"))
ex6_graph <- ggplot(daily_counts, aes(x = Date,  y = Daily, color = Country_Region)) + 
      geom_line() +
      ggtitle("Deaths by COVID-19") +
      geom_line(aes(group = seq_along(Date))) +
      transition_reveal(Date)

ex6_graph
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?